// 定义全局使用的常量
const path = require('path')
const packagePath = path.join(path.resolve('./'), 'package.json')
const { dependencies } = require(packagePath)
const port = 8010

// 定义要暴露的模块
module.exports = {
    /**
     * Webpack 配置入口
     * @param {*} param0
     * @returns
     */
    webpack({ webpackEnv, config }) {
        console.log('webpack', webpackEnv)

        // 配置 index.html
        config.plugin('html').tap(args => {
            args[0] = {
                ...args[0],
                ...{
                    title: 'Project-Name',
                    files: {}
                },
            }
            return args
        })

        return {
            devServer: {
                port: port,
            },
        }
    },

    /**
     * 打包相对路劲配置
     */
    moduleGenerator({ webpackEnv }) {
        console.log('moduleGenerator', webpackEnv)
        return webpackEnv === 'development' ? '/' : `http://localhost:${port}/`
    },

    /**
     * moduleFederation 配置
     */

    moduleFederation: {
        /**
         * 项目名称
         */
        name: 'ReactBase',

        /**
         * 暴露当前模块索引文件
         */
        filename: 'emp.js',

        /**
         * 引入远程模块入口
         */
        // remotes: {
        //     '@emp/react-project': `empReactProject@http://localhost:8011/emp.js`,
        // },

        /**
         * 暴露可以调用的模块
         * 被远程引用时的路径：本项目的相对路径
         */
        exposes: {
            './configs/index': 'src/configs/index',
            './compoents/index': 'src/index',
            './compoents/column': 'src/Column',
        },

        shared: { ...dependencies },

        /**
         * 共享 lib
         */
        // shared: {
        //     react: {singleton: true, requiredVersion: false},
        //     'react-dom' : {singleton: true, requiredVersion: false},
        // },
        shared: {
            react: { eager: true, singleton: true, requiredVersion: '^17.0.1' },
            'react-dom': { eager: true, singleton: true, requiredVersion: '^17.0.1' },
            'react-router-dom': { requiredVersion: '^5.1.2' },
        },
        // shared: Object.assign({}, shareByVersion('react'), shareByVersion('react-dom')),
    }
}
